/**
 * 处理生成json
 */
import Vue from 'vue'
// 把Array转json，key是code
FDUI.fieldsToObject = function(fields, key = 'code') {
  if (Array.isArray(fields)) {
    const objFields = {}
    fields.forEach(item => {
      objFields[item[key]] = item
    })
    return objFields
  } else {
    return fields
  }
}
// 把json转array
FDUI.fieldsToArray = function(fields) {
  if (Array.isArray(fields)) {
    return fields
  } else {
    return Object.keys(fields).map(i => fields[i])
  }
}
/**
 *
 * @param {*} field 字段
 * @param {*} config 配置项，放到__config__中
 * @param {*} params lookup参数
 * @param {*} data form参数
 * @returns
 */
function fieldToJson(field, config = {}, params = {}, data = {}) {
  const defaultConfig = {
    span: 8
  }
  config = Object.assign(defaultConfig, config)
  if (!field || !field.eleType) {
    return
  }
  const {
    eleType,
    eleDisType,
    code,
    name,
    isNull
  } = field
  const tag = `ele-distype-${eleDisType}`
  const fieldOption = {
    '__config__': {
      'key': code,
      'label': name,
      'tag': tag,
      'required': !isNull,
      ...config
    }
  }
  if (tag === FDUI.eleDisType.check.default) {
    fieldOption['true-label'] = 1
    fieldOption['false-label'] = 0
  }
  if (FDUI.eleType.CodeData == eleType || FDUI.eleType.LookUp == eleType) {
    const options = []
    console.log(data, code)
    // 存在值从refdata中拼接选项
    if (Object.hasOwnProperty.call(data, code)) {
      if (String(data[code]) !== '') {
        const codes = String(data[code]).split(',')
        const REFNAME = code + 'NAME'
        const refDatas = ((data['REFDATA'] && data['REFDATA'][REFNAME]) || '').split(',')
        codes.forEach((code, index) => {
          options.push({
            code: code,
            name: refDatas[index] || code
          })
        })
      }
    }
    fieldOption['__extend__'] = {
      options: options
    }
  }
  fieldOption['__server__'] = field
  fieldOption['__lookup__'] = params
  fieldOption['__data__'] = data
  return fieldOption
}
/**
 * 创建组成custom-form的json
 * @param {*} fields 所有字段
 * @param {*} form  给form初始值
 * @param {*} params lookup参数
 * @returns
 */
FDUI.createForm = function(fields, form, params = {}, data) {
  const defaultConfig = {
    colCount: 2
  }
  const prettyFields = []
  console.log(fields)
  fields.filter((field) => {
    return field.accessRight !== 0
  }).sort((pre, next) => {
    return pre.orderId >= next.orderId ? 1 : -1
  }).forEach((field) => {
    let span = 24 / defaultConfig
    if (field.colSpan) {
      span = field.colSpan
      // 兼容历史数据，后续可以删除
      if (span === 1) {
        span = 12
      } else if (span === 2) {
        span = 24
      }
    }
    const fieldOption = fieldToJson(field, {
      span: span
    }, params, data)
    Vue.set(form, field.code, '')
    prettyFields.push(fieldOption)
  })
  console.log(prettyFields)
  return [prettyFields]
}


FDUI.getLableWidth = function(fields) {
  fields = FDUI.fieldsToArray(fields)
  const sortedFields = fields.sort((pre, next) => pre.name.length > next.name.length ? -1 : 1)
  if (sortedFields && sortedFields.length) {
    return (sortedFields[0].name.length * 16 + 20) + 'px'
  } else {
    return '125px'
  }
}

FDUI.createGrid = function() {

}

FDUI.createGroups = function() {

}

FDUI.createTabs = function() {

}

export default {
  createForm: FDUI.createForm,
  createGrid: FDUI.createGrid,
  createGroups: FDUI.createGroups,
  createTabs: FDUI.createTabs
}
